/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.editor.message;

import org.eclipse.uml2.uml.Gate;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.OccurrenceSpecification;

/**
 * 
 * @author frankd@ifi.uio.no
 *
 * @todo Find a place to put these services
 */
public class MessageUtil {
	
	public static NamedElement getSender(Message message) {
		return getOwner(message.getSendEvent());
	}

	public static NamedElement getReceiver(Message message) {
		return getOwner(message.getReceiveEvent());
	}
	
	public static NamedElement getOwner(MessageEnd end) {
		if (end == null)
			return null;
		if (end instanceof OccurrenceSpecification) {
			OccurrenceSpecification event = (OccurrenceSpecification) end;
			return (NamedElement)
				(event.getCovereds().isEmpty() ? null : event.getCovereds().get(0));
		}
		else if (end instanceof Gate) {
			return (NamedElement) end.getOwner();
		}
		else
			throw new IllegalArgumentException("Unsupported send event: " + end);
	}
}
